<?php

namespace bgb;

use think\Log;

class Email
{
    
    /**
     * 邮件发送
     * @param string $userName 用户
     * @param string $name 名称
     * @param string $to 收件人
     * @param string $secret 秘钥
     * @param string $subject 主题
     * @param string $body 内容
     * @param array $cc 抄送
     * @param array $files 附件
     * @param string $port 端口
     * @param string $host 主机
     * @return boolean
     */
    protected function _send($userName, $name, $to, $secret, $subject, $body, $cc = [], $files = [], $port, $host)
    {
        $mail = new \PHPMailer\PHPMailer\PHPMailer(false);
        try {
            //Server settings
            $mail->SMTPDebug = \PHPMailer\PHPMailer\SMTP::DEBUG_OFF;                      // Enable verbose debug output
            $mail->isSMTP();                                            // Send using SMTP
            $mail->Host = $host;                    // Set the SMTP server to send through
            $mail->SMTPAuth = true;                                   // Enable SMTP authentication
            $mail->Username = $userName;                     // SMTP username
            $mail->Password = $secret;                               // SMTP password
            $mail->CharSet = "utf-8";
            $mail->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
            $mail->Port = $port;                                    // TCP port to connect to
            //Recipients
            $mail->setFrom($userName, $name);
            
            $mail->addAddress($to);
            
            if (!empty($cc)) foreach ($cc as $key => $item) {
                $mail->addCC($key, $item);
            }

            // Attachments
            if (!empty($files)) foreach ($files as $key => $item) {
                $mail->addAttachment($key, $item);
            }
            
            // Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body = $body;
            $mail->AltBody = $body;

            $mail->send();
            if (empty($mail->ErrorInfo)) {
                return true;
            } else {
                throw new \Exception($mail->ErrorInfo);
            }
        } catch (\Exception $e) {
            Log::write('发件人：'.$userName.',收件人：'.$to.',报错信息：'.$mail->ErrorInfo,'emailLog');
            return false;
        }
    }
    
    /**
     * 邮件发送
     * @param string $userName 用户
     * @param string $name 名称
     * @param string $to 收件人
     * @param string $secret 秘钥
     * @param string $subject 主题
     * @param string $body 内容
     * @param array $cc 抄送
     * @param array $files 附件
     * @return boolean
     */

    public function send($userName, $name, $to, $secret, $subject, $body, $cc = [], $files = [], $port, $host)
    {
        return $this->_send($userName, $name, $to, $secret, $subject, $body, $cc, $files, $port, $host);
    }

}
